<?php
class job{
	protected $id,$user,$project,$title,$description,$country,$ressourcetype,$ressourceext,$createdAt,$updatedAt;
	
	function __construct($id,$userid,$projectid,$title,$description,
						 $countryid,$ressourcetypeid,$ressourceextid,$createdAt,$updatedAt){
		$this->id = $id;
		$this->user = user::getUserById($userid);
		$this->project = project::getProjectById($projectid);
		$this->title = $title;
		$this->description = $description;
		$this->ressourcetype = ressourcetype::getRessourceTypeById($ressourcetypeid);
		$this->ressourceext = ressourceext::getRessourceExtById($ressourceextid);
		$this->country = country::getCountryById($countryid);
		$this->createdAt = $createdAt;
		$this->updatedAt = $updatedAt;
	}
	
	/*GETTTERS*/
	public function getId(){
		return $this->id;	
	}
	
	public function getUser(){
		return $this->user;	
	}
	
	public function getProject(){
		return $this->project;
	}

	public function getTitle(){
		return $this->title;	
	}

	public function getDescription(){
		return $this->description;	
	}

	public function getCountry(){
		return $this->country;	
	}
	
	public function getRessourceType(){
		return $this->ressourcetype;	
	}
	
	public function getRessourceExt(){
		return $this->ressourceext;	
	}
		
	public function getCreatedAt(){
		return $this->createdAt;	
	}

	public function getUpdatedAt(){
		return $this->updatedAt;	
	}

	public function setUser($var){
		$this->user = $var;
	}
	
	public function setProject($var){
		$this->project = $var;
	}

	public function setTitle($var){
		$this->title = $var;
	}

	public function setDescription($var){
		$this->description = $var;
	}

	public function setCountry($var){
		$this->country = $var;
	}

	public function setRessourceType($var){
		$this->ressourcetype = $var;	
	}
	
	public function setRessourceExt($var){
		$this->ressourceext = $var;	
	}
	
	public function toString(){
		echo "<br>";
		echo "ID: ".$this->id."<br>";
		echo "User: ".$this->user->getName()."<br>";
		echo "Title: ".$this->title."<br>";
		echo "Description: ".$this->description."<br>";
		echo "Country: ".$this->country->getName()."<br>";
		echo "Created At: ".date('y-m-d h:i:s',$this->createdAt)."<br>";
		echo "Updated At: ".date('y-m-d h:i:s',$this->updatedAt)."<br>";
	}
	
	public function getNumQuark(){
		$conn = new dbmodifier();
		$query = "SELECT id, COUNT(1) as 'number' FROM quark WHERE job_id = ".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		$row = $result->fetch_assoc();
		return $row['number'];		
	}
	public function getNumQuarkInProgress(){
		$conn = new dbmodifier();
		$query = "SELECT quark.id, COUNT(1) FROM quark RIGHT JOIN quarkresponse ON".
				" quarkresponse.quark_id = quark.id WHERE quark.job_id = ".$this->id." GROUP BY quark.id";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result->num_rows;	
	}
	
	public function getNumQuarkNotInProgress(){
		$conn = new dbmodifier();
		$query = "SELECT quark.id FROM quark LEFT JOIN quarkresponse ON quarkresponse.quark_id = quark.id".
				" WHERE quark.job_id = ".$this->id." AND ISNULL(quarkresponse.quark_id)";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result->num_rows;	
	}

	public function getNumQuarkCompleted(){
		$conn = new dbmodifier();
		$query = "SELECT *,COUNT(1) as 'number' FROM `quarkresolved` WHERE job_id =".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		$row = $result->fetch_assoc();
		return $row['number'];	
	}
	
	public static function getJobByUserId($userid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$query = "SELECT id, user_id, project_id, title, description, country_id, ressource_type_id, ressource_ext_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM JOB WHERE user_id =".$userid."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;
	}
	
	public static function getJobById($id){
		$conn = new dbmodifier();
		$id = $conn->db_connection->real_escape_string($id);
		$query = "SELECT id, user_id ,project_id, title, description, country_id,ressource_type_id, ressource_ext_id,".
					" UNIX_TIMESTAMP(created_at) AS 'created_at',".
					" UNIX_TIMESTAMP(updated_at) AS 'updated_at' FROM JOB WHERE id =".$id."";
		$result = $conn->updateSQL($query);
		$conn->execute();
		if(($result->num_rows)>0){
			$row = $result->fetch_assoc();			
			$var = new job($id,$row['user_id'],$row['project_id'],$row['title'],$row['description']
					,$row['country_id'],$row['ressource_type_id'],$row['ressource_ext_id']
					,$row['created_at'],$row['updated_at']);
		}else{
			$var = NULL;
		}
		return $var;			
	}
	
	public static function createJob($userid,$projectid,$title,$description,$countryid){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($userid);
		$projectid = $conn->db_connection->real_escape_string($projectid);		
		$title = $conn->db_connection->real_escape_string($title);
		$description = $conn->db_connection->real_escape_string($description);
		$countryid = $conn->db_connection->real_escape_string($countryid);
		$query = "INSERT INTO Job (user_id,project_id,title,description,country_id,ressource_type_id,".
				 "ressource_ext_id,created_at,updated_at) " .
				 " VALUES ('".$userid."','".$projectid."','".$title."','".
				 $description."','".$countryid."','0','0',FROM_UNIXTIME(".time()."),FROM_UNIXTIME(".time()."))";
		$id = $conn->updateSQLKey($query);
		$conn->execute();
		$var = new job($id,$userid,$projectid,$title,$description,$countryid,0,0,time(),time());
		return $var;		
	}
	
	public function save(){
		$conn = new dbmodifier();
		$userid = $conn->db_connection->real_escape_string($this->user->getId());
		$projectid = $conn->db_connection->real_escape_string($this->project->getId());		
		$title = $conn->db_connection->real_escape_string($this->title);
		$description = $conn->db_connection->real_escape_string($this->description);
		$countryid = $conn->db_connection->real_escape_string($this->country->getId());
		$ressourcetypeid = $conn->db_connection->real_escape_string($this->ressourcetype->getId());
		$ressourceextid = $conn->db_connection->real_escape_string($this->ressourceext->getId());
		$query = "UPDATE JOB SET user_id='".$userid."'";
		$query = $query.", project_id='".$projectid."'";	
		$query = $query.", title='".$title."'";
		$query = $query.", description='".$description."'";
		$query = $query.", country_id='".$countryid."'";
		$query = $query.", ressource_type_id='".$ressourcetypeid."'";
		$query = $query.", ressource_ext_id='".$ressourceextid."'";		
		$query = $query.", updated_at= FROM_UNIXTIME(".time().")";		
		$query = $query." WHERE id=".$this->id;
		$conn->updateSQL($query);
		$conn->execute();	
		$this->updated_at = time();		
	}
	
	public function delete(){
		$conn = new dbmodifier();
		$query = "DELETE FROM JOB WHERE id=".$this->id;
		$result = $conn->updateSQL($query);
		$conn->execute();
		return $result;		
	}
}
?>